from peewee import CharField, BigIntegerField, BigAutoField, DecimalField, IntegerField, DateField, TimeField
from playhouse.mysql_ext import JSONField

from common.model.abstrct import AbstractModel, IntEnumField
from enums.order_enum import (
    OrderStatusEnum, OrderComplaintStatusEnum, OrderShippingStatusEnum, OrderPayStatusEnum, OrderReturnStatusEnum,
    OrderItemComplaintStatusEnum, OrderItemReturnStatusEnum, OrderItemShippingStatusEnum,
    OrderSourceEnum, OrderTypeEnum, OrderComplaintTypeEnum

)


class Order(AbstractModel):
    class Meta:
        table_name = "tb_mall_order"
        indexes = (
            (("mall_id", "user_id"), False),
            (("order_sn", ), True),
            (("user_id", ), False)
        )

    order_id = BigAutoField(primary_key=True)
    mall_id = BigIntegerField(help_text="订单所属mall")
    order_source = IntEnumField(help_text="订单来源", choices=OrderSourceEnum)
    order_sn = CharField(max_length=32, help_text="订单sn,唯一")

    order_type = IntEnumField(help_text="订单类型", choices=OrderTypeEnum)

    order_status = IntEnumField(help_text="订单状态", choices=OrderStatusEnum)
    pay_status = IntEnumField(help_text="订单支付状态", choices=OrderPayStatusEnum)
    return_status = IntEnumField(help_text="订单退款状态", choices=OrderReturnStatusEnum)
    shipping_status = IntEnumField(help_text="订单物流状态", choices=OrderShippingStatusEnum)
    complaint_status = IntEnumField(help_text="订单售后状态", choices=OrderComplaintStatusEnum)
    complaint_type = IntEnumField(help_text="售后类型", choices=OrderComplaintTypeEnum, null=True)

    currency = CharField(max_length=3, help_text="币种", default="CNY")
    total_amount = DecimalField(default=0, help_text="订单总金额")
    paid_amount = DecimalField(default=0, help_text="订单支付金额")
    discount_amount = DecimalField(default=0, help_text="优惠金额")
    returned_amount = DecimalField(default=0, help_text="已退金额")
    freight_amount = DecimalField(default=0, help_text="运费")
    fee_amount = DecimalField(default=0, help_text="服务费")

    cost_point = IntegerField(default=0, help_text="消耗积分")

    user_id = BigIntegerField(help_text="用户id")
    # username = CharField(max_length=64, help_text="买家姓名")
    buyer_words = CharField(255, help_text="买家备注", null=True)

    receive_phone = CharField(128, help_text="收件人手机号", null=True)
    receive_name = CharField(128, help_text="收件人姓名", null=True)

    receive_province_id = BigIntegerField(help_text="省id", null=True)
    receive_province = CharField(128, help_text="省", null=True)
    receive_city_id = BigIntegerField(help_text="市id", null=True)
    receive_city = CharField(128, help_text="市", null=True)
    receive_area_id = BigIntegerField(help_text="区id", null=True)
    receive_area = CharField(128, help_text="区", null=True)
    receive_address = CharField(255, help_text="详细地址", null=True)

    voucher_snapshot_list = JSONField(help_text="使用的优惠券列表", default=[])
    promo_snapshot_list = JSONField(help_text="使用的促销列表", default=[])

    order_summary = CharField(max_length=255, help_text="订单概览,主要是用在下单的时候,传给支付第三方当作订单标题")

    def get_should_pay_amount(self):
        return self.total_amount - self.discount_amount + self.freight_amount + self.fee_amount


class OrderItem(AbstractModel):

    class Meta:
        table_name = "tb_mall_order_item"
        indexes = (
            (("order_id", ), False),
            (("mall_id", "order_id"), False),
            (("mall_id", "spu_id", "sku_id"), False)
        )

    item_id = BigAutoField(help_text="item id")
    order_id = BigIntegerField(help_text="订单id")
    mall_id = BigIntegerField(help_text="mall_id")
    spu_id = BigIntegerField(help_text="商品spu id")
    spu_name = CharField(max_length=64, help_text="商品名")
    sku_id = BigIntegerField(help_text="商品sku id")
    stock_id = BigIntegerField(help_text="扣减的库存所属记录行")
    quantity = IntegerField(help_text="商品数量")

    # item_status = IntEnumField(help_text="item 状态")
    item_return_status = IntEnumField(help_text="item 退款状态",
                                      choices=OrderItemReturnStatusEnum)
    item_shipping_status = IntEnumField(help_text="item 物流状态",
                                        choices=OrderItemShippingStatusEnum)
    item_complaint_status = IntEnumField(help_text="item 售后状态",
                                         choices=OrderItemComplaintStatusEnum)

    currency = CharField(max_length=3, help_text="币种", default="CNY")
    total_amount = DecimalField(default=0, help_text="总金额")
    unit_amount = DecimalField(default=0, help_text="商品单价")
    paid_amount = DecimalField(default=0, help_text="支付金额")
    discount_amount = DecimalField(default=0, help_text="折扣")
    returned_amount = DecimalField(default=0, help_text="已退金额")
    freight_amount = DecimalField(default=0, help_text="运费")
    fee_amount = DecimalField(default=0, help_text="服务费")

    cost_point = IntegerField(default=0, help_text="消耗积分")

    # 辅助信息
    voucher_snapshot_list = JSONField(help_text="使用的优惠券快照列表", default=[])
    promo_snapshot_list = JSONField(help_text="使用的促销快照列表", default=[])
    spu_snapshot = JSONField(help_text="商品spu快照", default={})
    sku_snapshot = JSONField(help_text="商品sku快照", default={})
    booking_date = DateField(help_text="商品需要选时间的时候的日期", null=True)
    start_time = TimeField(help_text="场次信息", null=True)
    end_time = TimeField(help_text="场次信息", null=True)










